package com.qipay.sp.excutor.xinyu;

import com.qipay.baas.model.Order;
import com.qipay.order.config.OrderStateEnum;
import com.qipay.order.service.OrderService;
import com.qipay.scalar.type.ApiTypeEnum;
import com.qipay.sp.excutor.AbstractExcutor;
import com.qipay.utils.DateUtils;
import com.qipay.utils.MD5Util;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.Map;

@Component
public class XyAlipayNativeExcutor extends AbstractExcutor {
    @Autowired
    OrderService orderService;
    private final static String API_URL="api_url";


    @Override
    protected ApiTypeEnum getApiTypeEnum() {
        return ApiTypeEnum.ALIPAY_NATIVE;
    }

    @Override
    protected Map<String, Object> getApiParams(Order order, Map<String, Object> spconfig, Map<String, Object> extendOrderParams) {
        Map<String, Object> apiParams = new HashMap<>();
        apiParams.put("inputCharset", "UTF-8");
        apiParams.put("payType", "3");//支付宝
        apiParams.put("orderId", order.getTradeNo());
        apiParams.put("transAmt", new BigDecimal(order.getAmount()).divide(new BigDecimal("100"),2,BigDecimal.ROUND_DOWN));
        apiParams.put("orderTime", DateUtils.sdf.format(order.getCreateTime()));
        apiParams.put("isPhone","1");
        return apiParams;
    }

    @Override
    protected Map<String, Object> run(Map<String, Object> spconfig, Map<String, Object> params) throws Exception {
        Object apiUrl = getVal(spconfig, API_URL);
        Object merchantId= getVal(spconfig, "merchantId");
        Object key= getVal(spconfig, "key");
        Object notifyUrl= getVal(spconfig, "notify_url");
        Object pageUrl= getVal(spconfig, "return_url");
        params.put("merchantId",merchantId);
        params.put("notifyUrl",notifyUrl);
        params.put("pageUrl",pageUrl);

        String sign = MD5Util.getSign(params,key.toString());
        params.put("sign",sign);

        String result = sendPostByKeyValue(params,apiUrl.toString());
        if (StringUtils.isBlank(result)) {
            throw new Exception("请求信誉的支付宝接口失败");
        }

        //储存接口请求返回的
        Order order = orderService.getByTradeNo(params.get("orderId").toString());
//        order.setRemark(result);

        Map<String, Object> resultMap = new HashMap<>();

        if (!result.contains("action")||!result.contains("orderId")) {
            logger.error("请求信誉的支付宝接口失败:" + result);
            resultMap.put("result_code", "1");
            resultMap.put("message", "请截图给上游客服：信誉接口请求失败");
            order.setState(OrderStateEnum.REQERROR.getType());
            orderService.update(order);
            return resultMap;
        }
        order.setState(OrderStateEnum.WAITING.getType());
        String[]contents = result.split("\"");
        resultMap.put("result_code", "0");
        resultMap.put("code_url",contents[5]);
        orderService.update(order);
        return resultMap;
    }
}
